function UpdateFileDatabases()

global Database;

disp('Reading filesystem...');

%% TimeFrequency database
disp('- TimeFrequency files');
Path = 'D:\ECoG\Data\Timefreq\';
files = dir([Path '* Timefreq data.mat']);
tfdata_header = {'Subject', 'Source', 'Condition', 'Datafile'};
tfdata = cell(length(files), 4);
for i=1:length(files)
    tfdata{i,strcmp(tfdata_header,'Subject')} = files(i).name(1:2);
    channelX = find(files(i).name==' ', 2, 'first');
    tfdata{i,strcmp(tfdata_header,'Source')} = files(i).name(channelX(1)+1:channelX(2)-1);
    tfdata{i,strcmp(tfdata_header,'Condition')} = files(i).name(channelX(2)+1:length(files(i).name)-18);
    tfdata{i,strcmp(tfdata_header,'Datafile')} = files(i).name;
end

Database.tfdata_header = tfdata_header;
Database.tfdata = tfdata;

%% MergedSources database
disp('- MergedSources files');
Path = 'D:\ECoG\Data\TimeFrequency Data\';
mSfiles = dir([Path '*MergedSources.mat']);
mergedsources_header = {'Subject', 'Channel', 'Condition', 'Datafile'};
mergedsources = cell(length(mSfiles), 4);
for i=1:length(mSfiles)
    load([Path mSfiles(i).name],'subject','channel','condition')
    mergedsources{i,strcmp(mergedsources_header,'Subject')} = subject;
    mergedsources{i,strcmp(mergedsources_header,'Channel')} = channel;
    mergedsources{i,strcmp(mergedsources_header,'Condition')} = condition;
    mergedsources{i,strcmp(mergedsources_header,'Datafile')} = mSfiles(i).name;
end
Database.mergedsources_header = mergedsources_header;
Database.mergedsources = mergedsources;

%% MergedFreqBins database
disp('- MergedFreqBins files');
mFfiles = dir([Path '*MergedFreqBins.mat']);
mergedfreqbins_header = {'Subject', 'Channel', 'Condition', 'Datafile'};
mergedfreqbins = cell(length(mFfiles), 4);
for i=1:length(mFfiles)
    load([Path mFfiles(i).name],'subject','channel','condition')
    mergedfreqbins{i,strcmp(mergedfreqbins_header,'Subject')} = subject;
    mergedfreqbins{i,strcmp(mergedfreqbins_header,'Channel')} = channel;
    mergedfreqbins{i,strcmp(mergedfreqbins_header,'Condition')} = condition;
    mergedfreqbins{i,strcmp(mergedfreqbins_header,'Datafile')} = mFfiles(i).name;
end
Database.mergedfreqbins_header = mergedfreqbins_header;
Database.mergedfreqbins = mergedfreqbins;

%% PeaksPerTrial database
disp('- PeaksPerTrial files');
mPPTfiles = dir([Path '*PeaksPerTrial.mat']);
peakspertrial_header = {'Subject', 'Channel', 'Condition', 'Datafile'};
peakspertrial = cell(length(mPPTfiles), 4);
for i=1:length(mPPTfiles)
    load([Path mPPTfiles(i).name],'subject','channel','condition')
    peakspertrial{i,strcmp(peakspertrial_header,'Subject')} = subject;
    peakspertrial{i,strcmp(peakspertrial_header,'Channel')} = channel;
    peakspertrial{i,strcmp(peakspertrial_header,'Condition')} = condition;
    peakspertrial{i,strcmp(peakspertrial_header,'Datafile')} = mPPTfiles(i).name;
end
Database.peakspertrial_header = peakspertrial_header;
Database.peakspertrial = peakspertrial;

end    